﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using V5_BLL;
using V5_Common;
using V5_Model;

namespace V5_EShop.Controllers {
    public class UserController : BaseController {

        new protected ModelUser User = new ModelUser();
        public UserController() {

            User = new BLLUser().GetModel(1);
            this.ViewData["user"] = User;

            string strWhere = "";
            strWhere = " memberid=" + User.ID;

            DataTable dt = new BLLProductOrder().GetList(10, strWhere, " OrderID desc").Tables[0];

            this.ViewData["order"] = dt.Rows;
        }

        // GET: Home
        public ActionResult Index() {
            return View();
        }

        public ContentResult Logout() {
            HttpCookie cookie1 = Request.Cookies["membername"];
            if (cookie1 != null) {
                cookie1.Expires = DateTime.Now.AddMinutes(-1.0);
                Response.Cookies.Add(cookie1);
            }
            HttpCookie cookie2 = Request.Cookies["memberid"];
            if (cookie2 != null) {
                cookie2.Expires = DateTime.Now.AddMinutes(-1.0);
                Response.Cookies.Add(cookie2);
            }
            Response.Redirect("/");
            return Content("处理成功!");
        }

        public ActionResult Login() {
            return View();
        }

        public ContentResult LoginPost() {

            DateTime time = DateTime.Now.AddHours(1);
            string userName = InputHelper.SetString(Request["UserName"]);
            string userpwd = InputHelper.SetString(Request["UserPwd"]);
            string chkremember = InputHelper.SetString(Request["chkremember"]);
            string returnUrl = InputHelper.SetString(Request["returnUrl"]);

            if (userName == "") {
                return Content("用户名称不能为空!");
            }
            if (userpwd == "") {
                return Content("用户密码不能为空!");
            }
            if (chkremember == "1") {
                time = DateTime.Now.AddDays(1);
            }
            BLLUser bll = new BLLUser();
            ModelUser model = new ModelUser();
            model.UserName = userName;
            model.UserPassword = InputHelper.MD5(userpwd, 16);
            model = bll.UserLogin(userName, InputHelper.MD5(userpwd, 16));
            if (model != null) {
                HttpCookie cookie = new HttpCookie("LoginStatus");
                cookie.Values.Add("account", model.UserName);
                cookie.Values.Add("pass", model.UserPassword);
                cookie.Expires = time;
                Response.Cookies.Add(cookie);
                HttpCookie cookie1 = new HttpCookie("membername", model.UserName);
                cookie1.Expires = time;
                Response.Cookies.Add(cookie1);

                HttpCookie cookie2 = new HttpCookie("memberid", model.ID.ToString());
                cookie2.Expires = time;
                Response.Cookies.Add(cookie2);
                if (returnUrl == "") {
                    Response.Redirect("/member/default.aspx");
                }
                else {
                    Response.Redirect(returnUrl);
                }
            }
            else {
                return Content("对不起!你的账号密码有误!或者您没有注册!");
            }
            return Content("处理成功!");
        }

        public ActionResult Reg() {
            return View();
        }

        [HttpPost]
        public ContentResult RegPost() {
            string txtUserName = InputHelper.SetString(Request["UserName"]);
            string txtUserPwd = InputHelper.SetString(Request["UserPwd"]);
            string txtUserPwd2 = InputHelper.SetString(Request["UserPwd2"]);
            string txtUserEmail = InputHelper.SetString(Request["UserEmail"]);
            string txtVerCode = InputHelper.SetString(Request["VerCode"]);
            BLLUser bll = new BLLUser();
            ModelUser model = new ModelUser();
            if (txtUserName == "") {
                return Content("用户名不能为空!");
            }
            if (txtUserPwd == "") {
                return Content("用户密码不能为空!");
            }
            if (txtUserPwd2 == "") {
                return Content("用户确认密码不能为空!");
            }
            if (txtUserPwd.Trim() != txtUserPwd2.Trim()) {
                return Content("两次输入的密码不相同");
            }
            if (txtUserEmail == "") {
                return Content("用户Email不能为空!");
            }
            if (txtVerCode == "") {
                return Content("验证码不能为空!");
            }
            if (txtVerCode != InputHelper.SetString(Session["LoginCode"])) {
                return Content("验证码输入不正确!");
            }

            if (!bll.CheckRegUser(txtUserName)) {
                return Content("该用户已经存在!请换一个试一试!");
            }

            model.ID = bll.GetMaxId();
            model.UserName = txtUserName;
            model.UserPassword = InputHelper.MD5(txtUserPwd, 16);
            model.UserEmail = txtUserEmail;
            model.RegDate = DateTime.Now;
            model.LoginDate = DateTime.Now;
            bll.Add(model);

            return Content("处理成功!");
        }

        //个人信息
        public ActionResult Info() {
            return View();
        }

        //个人信息POST
        public ContentResult InfoPost() {
            int id = InputHelper.SetInt(0);
            if (id > 0) {
                ModelUser model = new BLLUser().GetModel(id);
                model.TrueName = Request["UserName"];
                model.UserSex = InputHelper.SetInt(Request["UserSex"]);
                model.BirthDay = Request["UserBirthDay"];
                model.IdentityCard = Request["UserIdentityCard"];
                model.UserEmail = Request["UserEmail"];
                model.Telephone = Request["UserTelephone"];
                model.Phone = Request["UserPhone"];
                model.PostCode = InputHelper.SetInt(Request["UserPostCode"]);
                model.Address = "";
                model.Address2 = "";
                model.Address3 = "";
                model.Readme = Request["UserReadme"];
                new BLLUser().Update(model);
                return Content("恭喜您!修改成功!");
            }

            return Content("处理成功!");
        }

        public ActionResult Pwd() {
            return View();
        }

        public ContentResult PwdPost() {

            int id = InputHelper.SetInt("0");
            string UserOldPassword = InputHelper.SetString(Request["UserOldPassword"]);
            string UserPassword = InputHelper.SetString(Request["UserPassword"]);
            string UserPassword2 = InputHelper.SetString(Request["UserPassword2"]);

            if (UserOldPassword == "") {
                return Content("原密码不能为空!");
            }

            if (UserPassword == "" || UserPassword.Length == 0) {
                return Content("新密码不能为空!");
            }

            if (UserPassword2 == "" || UserPassword2.Length == 0) {
                return Content("确认密码不能为空!");
            }

            if (UserPassword != UserPassword2) {
                return Content("两次输入的密码不相同!");
            }

            if (id > 0) {
                ModelUser model = new BLLUser().GetModel(id);
                model.UserPassword = InputHelper.MD5(UserPassword, 16);
                new BLLUser().Update(model);
                return Content("密码修改成功!");
            }

            return Content("处理成功!");
        }

        //地址
        public ActionResult Address() {
            return View();
        }

        //地址Post
        public ContentResult AddressPost() {
            int id = InputHelper.SetInt(Request.Form["hfid"]);
            int userid = 0;
            string ToUserName = Request["ToUserName"];
            string Provinces = InputHelper.SetString(Request["province"]);
            string Cities = InputHelper.SetString(Request["City"]);
            string Counties = InputHelper.SetString(Request["County"]);
            string UserToUrl = InputHelper.SetString(Request["UserToUrl"]);
            string UserToPostNum = Request["UserToPostNum"];
            string UserToPhone = Request["UserToPhone"];
            string ToUserTelePhone = Request["ToUserTelePhone"];

            BLLUserToArea bll = new BLLUserToArea();
            ModelUserToArea model = new ModelUserToArea();
            model.tousername = ToUserName;
            model.touserareapro = Provinces;
            model.touserareacity = Cities;
            model.touserareacity2 = Counties;
            model.touserurl = UserToUrl;
            model.topostnum = UserToPostNum;
            model.touserphone = UserToPhone;
            model.tousertelephone = ToUserTelePhone;
            model.adddatetime = DateTime.Now;
            model.userid = userid;
            if (id == 0) {
                //检查一下最大的个数
                int count = bll.GetList(" userid=" + userid).Tables[0].Rows.Count;
                if (count < 4) {
                    model.id = bll.GetMaxId();
                    bll.Add(model);
                }
                else {
                    return Content("只能添加4个地址!请删除一个在添加!");
                }
            }
            else if (id > 0) {
                model.id = id;
                bll.Update(model);
                return Content("恭喜您!修改成功!");
            }
            return Content("修改成功!");
        }

        public ContentResult AddressDel() {
            var id = int.Parse(Request["id"]);
            new BLLUserToArea().Delete(id);
            return Content("配货地址删除成功!");
        }

        //订单列表
        public ActionResult OrderList() {
            return View();
        }

        public ActionResult OrderView(int id) {
            var m_productorder = new BLLProductOrder().GetModel(id);
            this.ViewData["view"] = m_productorder;
            return View();
        }

        public ActionResult Fav() {

            int Count = 0;
            DataTable dt = new BLLTemporary().GetList(
               20,
                1,
                " MemberID=" + 0,
                ref Count).Tables[0];

            return View();
        }

        public ActionResult Fav(int id) {

            BLLTemporary b_temporay = new BLLTemporary();
            ModelTemporary m_temporay = new ModelTemporary();

            if (b_temporay.ExistsByProductID(id)) {
                return Content("管理已经暂存成功!");
            }
            else {
                ModelProduct model = new BLLProduct().GetModel(id);
                m_temporay.ID = b_temporay.GetMaxId();
                m_temporay.ProductName = model.ProductName;
                m_temporay.ProductPrice = model.ProductMemberPrice;
                m_temporay.MemberID = 0;
                m_temporay.ProductID = id;
                m_temporay.AddDateTime = DateTime.Now;
                b_temporay.Add(m_temporay);
            }

            int Count = 0;
            DataTable dt = new BLLTemporary().GetList(
               20,
                1,
                " MemberID=" + 0,
                ref Count).Tables[0];

            return View();
        }

        public ContentResult FavPost(int id) {
            var action = Request["action"];
            switch (action) {
                case "del":
                    new BLLTemporary().Delete(id);
                    break;
                default:
                    break;
            }
            return Content("处理成功!");
        }

        public ActionResult ProductIntegral() {
            //这里添加订单的状态积分
            string strWhere = "";
            strWhere += " MemberID=" + 0;
            //还需要修改一下
            DataTable dt = new BLLProductOrder().GetList(10, strWhere, " OrderID Desc ").Tables[0];
            this.ViewData["list"] = dt.Rows;
            return View();
        }

        public ContentResult ProductIntegralPost() {
            return Content("处理成功!");
        }

        public ActionResult ProductComment() {

            string strWhere = "";
            int Count = 0;
            strWhere = " MemberID=0 and Passed=1 ";
            DataTable dt = new BLLProductComment().GetList(20,
                1,
                strWhere,
                ref Count).Tables[0];

            this.ViewData["list"] = dt.Rows;
            return View();
        }

        public ContentResult ProductCommentPost() {
            var user = new ModelUser();
            int int1 = 0, int2 = 0, int3 = 0, int4 = 0;
            string commentTitle = Request["CommentTitle"];
            string commentContent = Request["CommentContent"];
            int ProductID = InputHelper.SetInt(Request["ProductID"]);
            if (ProductID > 0) {
                ModelProductComment m_productcomment = new ModelProductComment();
                BLLProductComment b_productcomment = new BLLProductComment();
                m_productcomment.ID = b_productcomment.GetMaxId();
                m_productcomment.CommentName = commentTitle;
                m_productcomment.CommentContent = commentContent;
                m_productcomment.AddDateTime = DateTime.Now;
                m_productcomment.CommentEmail = user.UserEmail;
                m_productcomment.CommentIP = HttpHelper.GetIP;
                m_productcomment.MemberID = user.ID;
                m_productcomment.ProductID = ProductID;
                m_productcomment.Pmost = int1;
                m_productcomment.Pmost1 = int2;
                m_productcomment.Pmost2 = int3;
                m_productcomment.Pmost3 = int4;
                m_productcomment.Passed = 0;
                b_productcomment.Add(m_productcomment);
                Response.Redirect("/member/Evaluate_Comment.aspx");
            }
            return Content("处理成功!");
        }

        public ActionResult ProductComplaint() {

            return View();
        }

        public ContentResult ProductComplaintPost() {
            return Content("处理成功!");
        }

        public ActionResult ProductAsk() {
            var user = new ModelUser();
            string strWhere = "";
            int Count = 0;
            strWhere = " MemberID=" + user.ID;

            DataTable dt = new BLLProductAsk().GetList(10,
                1,
                strWhere,
                ref Count).Tables[0];

            this.ViewData["list"] = dt.Rows;

            return View();
        }

        public ContentResult ProductAskPost() {
            return Content("处理成功!");
        }

        public ActionResult ProductAnswer() {
            var user = new ModelUser();
            string strWhere = "";
            int Count = 0;
            strWhere = " MemberID=" + user.ID;

            DataTable dt = new BLLProductAnswer().GetList(10,
                1,
                strWhere,
                ref Count).Tables[0];

            this.ViewData["list"] = dt.Rows;

            return View();
        }

        public ContentResult ProductAnswerPost() {
            return Content("处理成功!");
        }
    }
}